﻿using Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HTTP
{
    class FormData
    {

        private string BoundStr = "-----------------------------7de1b519306dc";//根据抓包生成

        public byte[] build(List<ParameterItemNodeData> body)
        {
            #region 变量

            byte[] UploadBuffers = null;
            StringBuilder UploadBuf = new StringBuilder();

            #endregion

            UploadBuf.Append(BoundStr + "\r\n");
            int ii = 0;
            foreach (ParameterItemNodeData pin in body)
            {
                if (pin.Type == "File")
                {
                    if (ii > 0)
                    {
                        UploadBuf.Append("\r\n" + BoundStr + "\r\n");
                    }
                    #region 头部数据
                    UploadBuf.Append(@"Content-Disposition: form-data; name=""" + pin.Key + @"""; filename=""" + System.IO.Path.GetFileName(pin.Value) + @"""" + "\r\n");
                    UploadBuf.Append("Content-Type: " + pin.Parameter + "\r\n\r\n");
                    byte[] HeadBytes = Encoding.ASCII.GetBytes(UploadBuf.ToString());
                    byte[] PicBytes = FileBytes(pin.Value);
                    #endregion

                    UploadBuffers = ComposeArrays(HeadBytes, PicBytes);
                    ii += 1;
                }

            }

            #region 尾部数据
            UploadBuf.Clear();
            UploadBuf.Append("\r\n");
            foreach (ParameterItemNodeData pin in body)
            {
                if (pin.Type == "Text")
                {
                   UploadBuf.Append(BoundStr + "\r\n" + @"Content-Disposition: form-data; name=""" + pin.Key + @"""" + "\r\n\r\n" + pin.Value + "\r\n");
                }
            }
            UploadBuf.Append(BoundStr + "--");
            byte[] TailBytes = Encoding.ASCII.GetBytes(UploadBuf.ToString());
            #endregion

            UploadBuffers = ComposeArrays(UploadBuffers, TailBytes);

            return UploadBuffers;
        }

        public String getBoundStr()
        {
            return BoundStr;
        }

        private byte[] FileBytes(string fileName)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                byte[] buffur = new byte[fs.Length];
                fs.Read(buffur, 0, (int)fs.Length);

                return buffur;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (fs != null)
                {
                    //关闭资源  
                    fs.Close();
                }
            }
        }
        private byte[] ComposeArrays(byte[] Array1, byte[] Array2)
        {
            byte[] Temp = new byte[Array1.Length + Array2.Length];
            Array1.CopyTo(Temp, 0);
            Array2.CopyTo(Temp, Array1.Length);
            return Temp;
        }
    }
}
